package com.cx.base.day11_xml;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;

/**
 * @author shkstart
 * @date 8/20/2019 - 12:56 PM
 */
public class DomParseDemo {
    public static void main(String[] args) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = null;
        Document document = null;
        try {
            documentBuilder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        try {
            document = documentBuilder.parse("D:\\com.cx.java04.base.xmlnew.com.cx.java04.base.xml");
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Element rootElement = document.getDocumentElement();
        System.out.println(rootElement.getTagName());
        NodeList nodeList = document.getElementsByTagName("students");
        System.out.println(nodeList.getLength());
        NodeList subNodeList = document.getElementsByTagName("student");
        System.out.println(subNodeList.getLength());
    }
}
